<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/7/2
 * Time: 14:22
 */

namespace Shop\Api;


use Common\Api\Api;
use Shop\Model\WithdrawModel;

class WithdrawApi extends Api{

    /**
     * 查询，不分页
     */
    const QUERY_NO_PAGING = "Shop/Withdraw/queryNoPaging";
    /**
     * 查询，分页
     */
    const QUERY = "Shop/Withdraw/query";
    /**
     * 添加
     */
    const ADD = "Shop/Withdraw/add";
    /**
     * 保存
     */
    const SAVE = "Shop/Withdraw/save";
    /**
     * 获取信息
     */
    const GET_INFO = "Shop/Withdraw/getInfo";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "Shop/Withdraw/saveByID";

    /**
     * 删除
     */
    const DELETE = "Shop/Withdraw/delete";

	const PASS_WITHDRAW="Shop/Withdraw/passWithdraw";

    /*
     * 连表查询
     * */
    const WXUERQUERY = "Shop/Withdraw/Wxuserquery";

    protected function _init(){
        $this->model = new WithdrawModel();
    }
	
	
	/**
	 * 审核通过
	 */
	public function passWithdraw($map){
		
		$trans = M();
        $trans->startTrans(); //开启事务
		
		$result=apiCall(WithdrawApi::SAVE_BY_ID,array($map['id'],array('status'=>WithdrawModel::PASS)));
		if($result['status']){
			$result=apiCall(WithdrawApi::GET_INFO,array($map));

			$map=array(
				'uid'=>$result['info']['uid'],
			);

			$result1 = apiCall(WalletApi::GET_INFO_If_NOT_EXIST_THEN_ADD,array($map));
			$array = $result1['info'];
			$array['frozen_funds']=(float)$array['frozen_funds']-(float)$result['info'][0]['money'];

			$result2=apiCall(WalletApi::SAVE_BY_ID,array($array['id'],$array));
			if($result2['status']){
				$map=array(
				'uid'=>$result['info'][0]['uid'],
				'before_money'=>(float)$array['account_balance']+(float)$result['info'][0]['money'],
				'plus'=>0,
				'minus'=>$result['info'][0]['money'],
				'after_money'=>$array['account_balance'],
				'create_time'=>time(),
				'dtree_type'=>getDatatree('COMMISSION_CHECK_PASS'),
				'reason'=>'审核通过'
				);
				$result=apiCall(WalletHisApi::ADD,array($map));
				if($result['status']){
					$trans->commit();//提交事务
					return $result;
				}else{
					$trans->rollback();//回滚事务
					return $result['info'];
				}
			}else{
				$trans->rollback();//回滚事务
				return $result2['info'];
			}
			
		}else{
			$trans->rollback();//回滚事务
			return $result['info'];
		}
		
	}

	/*
	 * 连表查询获得相应的体现的用户信息
	 * */
	public function Wxuserquery($map = null, $page = array('curpage'=>0,'size'=>10), $order = false, $params = false, $fields = false){
        $query = $this->model->join('INNER JOIN itboye_wxuser ON itboye_wxuser.id = itboye_withdraw.uid');
        if(!is_null($map)){
            $query = $query->where($map);
        }
        if(!($order === false)){
            $query = $query->order($order);
        }
        if(!($fields === false)){
            $query = $query->field($fields);
        }
        $list = $query -> page($page['curpage'] . ',' . $page['size']) -> select();


        if ($list === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $count = $this -> model -> where($map) -> count();
        // 查询满足要求的总记录数
        $Page = new \Think\Page($count, $page['size']);

        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }

        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page -> show();

        return $this -> apiReturnSuc(array("show" => $show, "list" => $list));


    }

}